package com.gitee.wsl.compose.ui.richtext.edit.state

import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isAltPressed
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.isMetaPressed
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type


/**
 * Increases and decreases the list level of the current selected lists when the Tab key is pressed.
 *
 * @param event the key event.
 * @return true if the list level was increased or decreased, false otherwise.
 */
internal fun RichTextState.onPreviewKeyEvent(event: KeyEvent): Boolean {
    if (event.type != KeyEventType.KeyDown)
        return false

    if (event.key != Key.Tab)
        return false

    if (
        event.isMetaPressed ||
        event.isCtrlPressed ||
        event.isAltPressed
    )
        return false

    if (!isList)
        return false

    if (event.isShiftPressed && canDecreaseListLevel())
        decreaseListLevel()
    else if (!event.isShiftPressed && canIncreaseListLevel())
        increaseListLevel()
    else
        return false

    return true
}
